package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.exception.DeletionNotAllowedException;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveDishWithFlavor(DishDto dishDto) {

        save(dishDto);
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(dishFlavor -> {
            dishFlavor.setDishId(id);
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }


    public void batchDeleteDish(List<Long> ids) {
        //判断是否有未停售的·
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(ids != null && !ids.isEmpty(), Dish::getId, ids);
        List<Dish> dishList = list(dishLambdaQueryWrapper);
        for (Dish dish : dishList) {
            if (dish.getStatus() == 1) {
                throw new DeletionNotAllowedException("菜品在售无法删除");
            }
        }
        removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null, DishFlavor::getDishId, ids);
        dishFlavorService.remove(queryWrapper);
    }

    @Override
    public DishDto getByIdDishWithFlavor(Long id) {
        Dish dish = getById(id);
        DishDto dishDto = new DishDto();
        BeanUtil.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorService.list(
                new LambdaQueryWrapper<DishFlavor>().eq(id !=null,DishFlavor::getDishId,id)));
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDto,dish);
        updateById(dish);

        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors= flavors.stream().peek(dishFlavor -> dishFlavor.setDishId(id)).collect(Collectors.toList());
        if (!flavors.isEmpty()){
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(id != null, DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
            dishFlavorService.saveBatch(flavors);
        }
    }
}
